//
//  XLVistorView.swift
//  微博
//
//  Created by macbook on 16/9/29.
//  Copyright © 2016年 buWangChuXing. All rights reserved.
//

import UIKit
import SnapKit

class XLVistorView: UIView {

    //闭包
    var close :(()->())?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    //设置ui
    func setupUI(){
        
        
        backgroundColor = RBG(237, green: 237, blue: 237)
        //添加控件
        addSubview(feedImageView)
        addSubview(maskImageView)
        addSubview(hoseImageView)
        addSubview(textLabel)
        addSubview(loginBtn)
        addSubview(registerBtn)
        
        //添加约束
        feedImageView.snp_makeConstraints { (make) in
            
            make.center.equalTo(self)
        }
        
        maskImageView.snp_makeConstraints { (make) in
            
            make.center.equalTo(self)
        }
        
        hoseImageView.snp_makeConstraints { (make) in
            
            make.center.equalTo(self)
        }
        
        textLabel.snp_makeConstraints { (make) in
            
            make.width.equalTo(224)
            make.height.equalTo(36)
            make.top.equalTo(maskImageView.snp_bottom).offset(16)
            make.centerX.equalTo(maskImageView)

        }
        loginBtn.snp_makeConstraints { (make) in
            
            make.size.equalTo(CGSize(width: 100, height: 36))
            make.left.equalTo(textLabel)
            make.top.equalTo(textLabel.snp_bottom).offset(16)
        }
        
        registerBtn.snp_makeConstraints { (make) in
            
            make.size.equalTo(CGSize(width: 100, height: 36))
            make.right.equalTo(textLabel)
            make.top.equalTo(textLabel.snp_bottom).offset(16)
        }

    
        
    }
    
    //懒加载圆圈  这样写比较麻烦,可以使用便利构造函数
    private lazy var feedImageView:UIImageView = UIImageView(image:UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    //遮罩
    private lazy var maskImageView:UIImageView = UIImageView(imageName: "visitordiscover_feed_mask_smallicon");
    //房子
    private lazy var hoseImageView : UIImageView = UIImageView(imageName: "visitordiscover_feed_image_house")
    
    //文字
    private lazy var textLabel : UILabel = {
    
        let label = UILabel()
        label.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        label.numberOfLines = 0
        label.font = UIFont.systemFontOfSize(14)
        label.textAlignment = NSTextAlignment.Center

        return label;
        
    }()
    
    //两个按钮
    //登陆
    lazy var loginBtn: UIButton = {
    
        let logBtn = UIButton()
        logBtn.setTitle("登录", forState: .Normal)
        logBtn.setTitleColor(ThemeColor, forState: UIControlState.Normal)
        logBtn.setBackgroundImage(UIImage(named:"common_button_white_disable"), forState: UIControlState.Normal)
        logBtn.titleLabel?.font = UIFont.systemFontOfSize(14)
        logBtn.addTarget(self, action:#selector(XLVistorView.loginBtnClick), forControlEvents: .TouchUpInside)
        return logBtn
    }()
    
    //注册
    lazy var registerBtn: UIButton = {
        
        let btn = UIButton()
        btn.setTitle("登录", forState: UIControlState.Normal)
        btn.setTitleColor(ThemeColor, forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named:"common_button_white_disable"), forState: UIControlState.Normal)
        btn.titleLabel?.font = UIFont.systemFontOfSize(14)
        
        //添加方法
        btn.addTarget(self, action: #selector(XLVistorView.loginBtnClick), forControlEvents: UIControlEvents.TouchUpInside)
        
        return btn
    }()
    
    
    //按钮方法的实现
    func loginBtnClick(){
    
        close?()
    }
    
    //旋转
    func setAnimate(){
    
        let animate = CABasicAnimation(keyPath: "transform.rotation")
        animate.duration = 15
        animate.toValue = M_PI_2 * 2
        animate.repeatCount = MAXFLOAT
        //当app推送后台或者切换控制器的实际,默认动画完成
        animate.removedOnCompletion = false
        
        ///添加动画
        feedImageView.layer.addAnimation(animate, forKey: nil)
        
    }
    
    //供外界调用
    func setupVistorInfor(imageName:String?,title:String?){
    
        //如果有值就不是主页
        if let image = imageName ,let tit = title{
            
            hoseImageView.image = UIImage(named: image)
            textLabel.text = tit
            
            //影藏圆圈
            feedImageView.hidden = true
        }else
        {
            setAnimate()
        }
       
    }
    
    
    
}

















